/*
 * adapter.h
 *
 *  Created on: 2011-7-25
 *      Author: minorityee1984@gmail.com
 *     Purpose: CIOAdapterBase, IO適配器基類
 *        Note:
 */

#ifndef ADAPTER_H_
#define ADAPTER_H_

#include "type_def.h"
#include "receiver.h"
#include "sender.h"
#include "common/common_bytequeue.h"
#include <assert.h>

typedef union tagIOInfo
{
	int sockfd;
}IOInfo;

class CIOAdapterBase
{
public:
	CIOAdapterBase(IOInfo ioInfo, IReceiver *pReceiver, ISender *pSender)
	: m_IOInfo(ioInfo), m_pReceiver(pReceiver), m_pSender(pSender){}

	virtual ~CIOAdapterBase(){}

	virtual int Initialize()
	{
		if (NULL == pReceiver || NULL == pSender)
		{
			return E_NULLPTR;
		}

		int ret = m_pReceiver->Initialize();
		if (0 > ret)
		{
			return ret;
		}

		ret = m_pSender->Initialize();
		if (0 > ret)
		{
			return ret;
		}
		return S_OK;
	}
	virtual int Uninitialize() = 0;

	int RegAdapter(CIOAdapterBase* pAdapter)
	{
		assert(NULL != pAdapter);
		m_pSender = pAdapter;
	}

	uint16_t Receive(Byte *pszBuf, uint16_t &uLen)
	{
		assert(NULL != m_pReceiver);
		return m_pReceiver->Receiver(pszBuf, uLen);
	}
	uint16_t Send(const Byte *pszBuf, uint16_t &uLen)
	{
		assert(NULL != m_pSender);
		return m_pSender->Send(pszBuf, uLen);
	}
private:
	IOInfo m_IOInfo;
	CByteQueue<0x100> m_buf;
};


#endif /* ADAPTER_H_ */
